﻿// Original author contact info: Owen Emlen (owene_1998@yahoo.com)
// Note: other individuals may also have contributed to this code
// Project hosted on CodePlex.com as of 1/10/2009 at http://www.codeplex.com/EmlenMud
using System;
using System.Diagnostics;
using System.Reflection;
using BrainTechLLC.ThreadSafeObjects;
using LinFu.AOP.Interfaces;

namespace BrainTechLLC
{
	[Serializable, AttributeUsage(AttributeTargets.Method)]
	public class TimeMethodAttribute : Attribute, IReadableAttribute
	{
		public string RecorderMethod { get; set; }
		public string AssociatedString { get; set; }
		public object Value { get { return AssociatedString; } }

		public TimeMethodAttribute(string recorderMethod, string associatedString)
		{
			RecorderMethod = recorderMethod;
			AssociatedString = associatedString;
		}

		public void DoWorkAfter(IInvocationContext context, object thisObject, Stopwatch watch)
		{
			if (!string.IsNullOrEmpty(RecorderMethod))
			{
				Type t = thisObject.GetType();
				MethodInfo methodInfo = Lookups.GeneralLookup.Get(t.DotMethodName(RecorderMethod), v => t.GetMethod(RecorderMethod));
				methodInfo.InvokeIfNotNull(thisObject, context, watch);
			}
		}
	}
}

